export function calculateAge(birthdate: string): number {
  // 将生日字符串转换为日期对象
  const birthDate = new Date(birthdate)
  const today = new Date()
  // 计算年龄
  let age = today.getFullYear() - birthDate.getFullYear()
  // 检查是否已经过了生日
  if (
    today.getMonth() < birthDate.getMonth() ||
    (today.getMonth() === birthDate.getMonth() && today.getDate() < birthDate.getDate())
  ) {
    age--
  }
  return age
}
//得到今天是星期几
export function getDayOfWeek(): number {
  const today = new Date()
  return today.getDay()
}
//得到今天是第几周
export function getWeekNumber(): number {
  const date = new Date()
  const today = new Date(date.getFullYear(), date.getMonth(), date.getDate())
  const targetDate = new Date(today.getFullYear(), 8, 4)

  //学期开始的周数
  const targetWeekNumber = Math.ceil(targetDate.getDate() / 7)

  //计算今天所在的周数
  const diffDays = Math.ceil((today.getTime() - targetDate.getTime()) / (24 * 60 * 60 * 1000))
  const weekNumber = targetWeekNumber + Math.floor(diffDays / 7)

  return weekNumber
}
//得到某一周的日期
export function getWeek(weekNumber: number): Date[] {
  const today = new Date()
  const targetDate = new Date(today.getFullYear(), 8, 4) // 9/4 号作为第一周的基准日期

  // 计算目标日期（9/4 号）所在的周数
  const targetWeekNumber = Math.ceil(targetDate.getDate() / 7)

  // 计算当前周数相对于基准日期的偏移量
  const weekOffset = weekNumber - targetWeekNumber

  // 计算当前周的第一天日期
  const firstDayOfWeek = new Date(targetDate.getTime() + weekOffset * 7 * 24 * 60 * 60 * 1000)

  // 获取当前周的每一天日期
  const daysInWeek = []
  for (let i = 0; i < 7; i++) {
    const currentDate = new Date(firstDayOfWeek.getTime() + i * 24 * 60 * 60 * 1000)
    daysInWeek.push(currentDate)
  }
  return daysInWeek
}
//得到教学周历(二维数组呈现)
export function getCalendar(): Date[][] {
  const time = [] as Date[][]
  for (let i = getWeekNumber(); i < getWeekNumber() + 5; i++) {
    const week = getWeek(i)
    const weekTime = []
    for (let j = 0; j < 7; j++) {
      weekTime.push(week[j])
    }
    time.push(weekTime)
  }
  return time
}
